package net.kong.sns.rest.client.friend;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.kong.sns.rest.client.InvalidResponseException;
import net.kong.sns.rest.client.KongApiMethod;
import net.kong.sns.rest.client.method.JsonMethodInvokeBase;
import net.kong.sns.rest.client.model.FriendInfo;
import net.kong.sns.rest.client.rest.HttpResponseContent;


/**
 * @author yk
 * @version $Id: GetFriendsComplexMethodInvoker.java,v 1.2 2009/11/04 17:17:38 ningyubin Exp $
 */
public class GetFriendsComplexMethodInvoker extends JsonMethodInvokeBase<List<FriendInfo>>{

    static Set<String> initializedFields = new HashSet<String>(){{add("user_id");add("nickname");add("male");add("female");}};
  
    public GetFriendsComplexMethodInvoker(KongApiMethod method, String sessionKey) {
        super(method, sessionKey);
        setParameter("format", "1");
    }
    
    public GetFriendsComplexMethodInvoker(KongApiMethod method, String sessionKey,Long user_id) {
        super(method, sessionKey);
        if (user_id != null) setParameter("user_id", user_id.toString());
        setParameter("format", "1");
    }

    @Override
    protected List<FriendInfo> processJsonResponse(HttpResponseContent responseData) throws InvalidResponseException {
        String response = responseData.getBody();
        Object o = reader.read(response);
        List<Map<String, Object>> relationList = (List<Map<String, Object>>) o;
        if (relationList.size() > 0) {
            List<FriendInfo> ret=new ArrayList<FriendInfo>();
            for(Map<String, Object> afriendInfoJson:relationList){
                ret.add(trans(afriendInfoJson));
            }
            return ret;
        }
        return null;
    }

    private FriendInfo trans(Map<String, Object> arelationjson){
        FriendInfo friendInfo = new FriendInfo();
        friendInfo.initialize(arelationjson, initializedFields);
        return friendInfo;
    }
}
